package com.example.flutter_desktop_alert;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.EventChannel.EventSink;


import java.util.HashMap;
import java.util.Map;

/** FlutterDesktopAlertPlugin */
public class FlutterDesktopAlertPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private static Context context;
  private Activity activity;

  // 事件派发对象
  private  EventChannel.EventSink eventSink = null;
  // 事件派发流
  private  EventChannel.StreamHandler streamHandler = new EventChannel.StreamHandler() {
    @Override
    public void onListen(Object arguments, EventSink events) {
      eventSink = events;
    }

    @Override
    public void onCancel(Object arguments) {
      eventSink = null;
    }
  };

  @Override
  public void onAttachedToActivity(ActivityPluginBinding binding) {
    activity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    this.onDetachedFromActivity();
  }

  @Override
  public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
    this.onAttachedToActivity(binding);
  }

  @Override
  public void onDetachedFromActivity() {
    activity = null;
  }

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_desktop_alert");
    channel.setMethodCallHandler(this);

    // 初始化事件
    EventChannel eventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_desktop_alert/event");
    eventChannel.setStreamHandler(streamHandler);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    }
    else if (call.method.equals("creatDesktop")) {

      System.out.print("android层：creatDesktop");
      String shareCount = call.argument("shareCount");
      String money = call.argument("money");
      MyAppWidgetProvider.accept(shareCount,money);
      CjActivity.Cj_AppWeight(activity.getApplicationContext(),shareCount,money);
      if (eventSink != null) {
        Map map = new HashMap();
        map.put("event","initCall");
        map.put("isSucc","1");
        map.put("money",money);
        map.put("shareCount",shareCount);
        eventSink.success(map);
      }
    }
    else if (call.method.equals("updateDesktopWidget")) {
      System.out.print("android层：updateDesktopWidget");
      String shareCount = call.argument("shareCount");
      String money = call.argument("money");
      CjActivity.get_BS(activity.getApplicationContext(),shareCount,money);
      if (eventSink != null) {
        Map map = new HashMap();
        map.put("event","updateCall");
        map.put("isSucc","1");
        map.put("money",money);
        map.put("shareCount",shareCount);
        eventSink.success(map);
      }
    }
    else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
